﻿using HCQ2_IBLL;
using HCQ2_Model;
using HCQ2_Model.VLine.InputDto._0416;
using HCQ2_Model.VLine.OutputDto._0416;
using HCQ2_Model.VRDD.InputDto._0714;
using HCQ2_Model.VRDD.OutputDto._0714;
using HCQ2_Model.VRDD.OutputDto._210908;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace HCQ2_BLL
{
    public partial class T_VRGoodDiscountBLL: IT_VRGoodDiscountBLL
    {
        /// <summary>
        ///  获取商品折扣信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<GoodRateResult> GetReducedRate(GoodRatePram param)
        {
            List<T_VRGoodDiscount> rateList;
            if (param.file_id>0)
                rateList = DBSession.IT_VRGoodDiscountDAL.Select(s => s.file_id == param.file_id);
            else
                rateList = DBSession.IT_VRGoodDiscountDAL.Select(s => true);
            List<GoodRateResult> list = new List<GoodRateResult>();
            if (rateList == null || rateList.Count() <= 0)
                return null;
            List<int> fileId = rateList.Select(s => s.file_id).ToList();
            HashSet<int> fId = new HashSet<int>(fileId);//排除重复
            foreach(int i in fId)
            {
                List<T_VRGoodDiscount> q = rateList.Where(n => n.file_id == i).ToList();
                List<GoodRateDetial> r = new List<GoodRateDetial>();
                q.ForEach(s => r.Add(new GoodRateDetial
                {
                    user_level = s.user_level,
                    rate = s.rate,
                    note = s.note
                }));
                list.Add(new GoodRateResult
                {
                    file_id = i,
                    rateList = r
                });
            }
            return list;
        }
        /// <summary>
        ///  获取创始商铺信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<FolderInfoResult> GetFolderInfo(FolderInfoParam param)
        {
            List<FolderInfoResult> result = new List<FolderInfoResult>();
            List<FolderOutput> list = DBSession.IT_VRGoodDiscountDAL.GetFolderOutput(param);
            if (list == null || list.Count <= 0)
                return result;
            list.ForEach(s =>
            {
                //商铺推荐商品
                var fList = DBSession.IT_DocumentInfoDAL.Select(o => o.store_id == s.folder_id && o.page_type.Equals("DDCircle") && o.store_top && o.file_status==5 && !o.if_remove, o => o.update_time, false);
                List<RecGood> t = new List<RecGood>();
                if(fList!=null && fList.Count>0)
                {
                    fList.ForEach(o => 
                    {
                        //商品缩略图信息
                        List<T_DocBagFileRecord> bag = DBSession.IT_DocBagFileRecordDAL.Select(n => n.file_id == o.file_id);
                        bag?.ForEach(m =>
                        {
                            if (!string.IsNullOrEmpty(m.file_url))
                                m.file_url = m.file_url.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
                        });
                        t.Add(new RecGood
                        {
                            file_id = o.file_id,
                            file_name = o.file_name,
                            file_note = o.file_note,
                            photoOption = bag
                        });
                    });
                }
                result.Add(new FolderInfoResult
                {
                    city = s.city,
                    folder_id = s.folder_id,
                    folder_name = s.folder_name,
                    folder_note = s.folder_note,
                    folder_path = s.folder_path,
                    folder_url = (!string.IsNullOrEmpty(s.folder_url)) ? s.folder_url.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/"))) : s.folder_url,
                    num = s.num,
                    sale = s.sale,
                    partner_num = s.partner_num,
                    apply_num = s.apply_num,
                    rec_list = t
                });
            });
            return result;
        }
        /// <summary>
        ///  获取合伙商铺
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<ApplyFolderResult> GetApplyFolderInfo(FolderInfoParam param)
        {
            List<ApplyFolderResult> result = new List<ApplyFolderResult>();
            List<ApplyFolder> list = DBSession.IT_VRGoodDiscountDAL.GetApplyFolder(param);
            if (list == null || list.Count <= 0)
                return result;
            list.ForEach(s=> {
                //商铺推荐商品
                var fList = DBSession.IT_DocumentInfoDAL.Select(o => o.store_id == s.folder_id && o.page_type.Equals("DDCircle") && o.store_top, o => o.update_time, false);
                List<RecGood> t = new List<RecGood>();
                if (fList != null && fList.Count > 0)
                {
                    fList.ForEach(o =>
                    {
                        //商品缩略图信息
                        List<T_DocBagFileRecord> bag = DBSession.IT_DocBagFileRecordDAL.Select(n => n.file_id == o.file_id);
                        bag?.ForEach(m =>
                        {
                            if (!string.IsNullOrEmpty(m.file_url))
                                m.file_url = m.file_url.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
                        });
                        t.Add(new RecGood
                        {
                            file_id = o.file_id,
                            file_name = o.file_name,
                            file_note = o.file_note,
                            photoOption = bag
                        });
                    });
                }
                result.Add(new ApplyFolderResult
                {
                    folder_id = s.folder_id,
                    folder_name = s.folder_name,
                    folder_note = s.folder_note,
                    folder_path = s.folder_path,
                    folder_url = (!string.IsNullOrEmpty(s.folder_url)) ? s.folder_url.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/"))) : s.folder_url,
                    apply_date = s.apply_date,
                    apply_num = s.apply_num,
                    gold_num = s.gold_num,
                    line_num = s.line_num,
                    ref_name = s.ref_name,
                    sale = s.sale,
                    score_num = s.score_num,
                    user_level = s.user_level,
                    rec_list = t
                });
            });
            return result;
        }
        /// <summary>
        ///  获取未关联
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<MaterialResult> GetMaterialResult(MaterialParam param)
        {
            List<MaterialResult> result = new List<MaterialResult>();
            List<MaterialOutput> list = DBSession.IT_VRGoodDiscountDAL.GetMaterialOutput(param);
            if (list == null || list.Count <= 0)
                return result;
            list.ForEach(s =>
            {
                //商品缩略图信息
                List<T_DocBagFileRecord> bag = DBSession.IT_DocBagFileRecordDAL.Select(n => n.file_id == s.file_id);
                bag?.ForEach(m =>
                {
                    if (!string.IsNullOrEmpty(m.file_url))
                        m.file_url = m.file_url.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
                });
                result.Add(new MaterialResult
                {
                    file_id = s.file_id,
                    file_name = s.file_name,
                    photoOption = bag
                });
            });
            return result;
        }
        /// <summary>
        ///  获取和指定商品未关联的素材
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<UnlinkedMaterialOutput> GetUnlinkedMaterial(UnlinkedMaterialParam param)
        {
            var list= DBSession.IT_VRGoodDiscountDAL.GetUnlinkedMaterial(param);
            list?.ForEach(s =>
            {
                //商品缩略图信息
                List<T_DocBagFileRecord> bag = DBSession.IT_DocBagFileRecordDAL.Select(n => n.file_id == s.file_id);
                bag?.ForEach(m =>
                {
                    if (!string.IsNullOrEmpty(m.file_url))
                        m.file_url = m.file_url.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
                });
                s.photoOption = bag;
            });
            return list;
        }

        //******************************************20210714需求********************************************
        /// <summary>
        ///  获取店铺所有商品的折扣
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<OutputGoodRate> GetGoodsRateByStore(InputGoodRate param,int user_id)
        {
            List<OutputGoodRate> list = DBSession.IT_VRGoodDiscountDAL.GetGoodRateList(param, user_id);
            if (list == null || list.Count <= 0)
                return null;
            list.ForEach(s =>
            {
                //计算0级折扣价
                if (s.rate0 > 0)
                    s.ratePrice = Math.Round(s.filePrice * s.rate0 / 10, 2);
                else
                    s.ratePrice = s.filePrice;
                //1、折扣列表
                List<GoodRateList> rate = new List<GoodRateList>();
                List<T_VRGoodDiscount> r = DBSession.IT_VRGoodDiscountDAL.Select(o => o.file_id == s.file_id);
                r?.ForEach(n => {
                    rate.Add(new GoodRateList
                    {
                        id = n.id,
                        user_level = n.user_level,
                        rate = n.rate,
                        note = n.note
                    });
                });
                s.rate = rate;
                //2、商品规格列表
                var obj = DBSession.IT_VRDDGoodSpecificationDAL.Select(o => o.file_id == s.file_id);
                List<GoodsSizeModel> spec = new List<GoodsSizeModel>();
                obj?.ForEach(n => spec.Add(new GoodsSizeModel { specName = n.specName, specOption = n.specOption }));
                s.specList = spec.Count > 0 ? spec : null;
                //3、缩略图列表
                List<T_DocBagFileRecord> photoOption = DBSession.IT_DocBagFileRecordDAL.Select(o => o.file_id == s.file_id);
                if (photoOption.Count > 0)
                {
                    photoOption.ForEach(o =>
                    {
                        o.file_url = o.file_url.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
                        if (o.file_type == 0)
                            s.fileImage = o.file_url;
                    });
                }
                s.photoOption = photoOption;
            });
            return list;
            //原始
            //List<T_VRGoodDiscount> list = DBSession.IT_VRGoodDiscountDAL.Select(s => s.store_id == param.store_id);
            //if (list == null || list.Count <= 0)
            //    return null;
            //List<OutputGoodRate> result = new List<OutputGoodRate>();
            //var g = list.Select(s => new { s.file_id, s.file_name }).ToList();
            //g.ForEach(o =>
            //{
            //    OutputGoodRate temp = new OutputGoodRate
            //    {
            //        file_id = o.file_id,
            //        fileName = o.file_name
            //    };
            //    //折扣列表
            //    var r = list.Where(n => n.file_id == o.file_id)?.ToList();
            //    List<GoodRateList> rate = new List<GoodRateList>();
            //    r?.ForEach(n => {
            //        rate.Add(new GoodRateList
            //        {
            //            id = n.id,
            //            user_level = n.user_level,
            //            rate = n.rate,
            //            note = n.note
            //        });
            //    });
            //    temp.rate = rate;
            //    //缩略图列表
            //    List<T_DocBagFileRecord> photoOption = DBSession.IT_DocBagFileRecordDAL.Select(s => s.file_id == o.file_id);
            //    if (photoOption.Count > 0) {
            //        photoOption.ForEach(s =>
            //        {
            //            s.file_url = s.file_url.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
            //            if (s.file_type == 0)
            //                temp.fileImage = s.file_url; 
            //        });
            //    }
            //    temp.photoOption = photoOption;
            //    result.Add(temp);
            //});
            //return result;
        }
    }
}